package com.jianyun.jyzs.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.jianyun.jyzs.R;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.UtilsStyle;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by mlli on 2017/11/10.
 * 查看大图
 */

public class PicturePreviewActivity extends AppCompatActivity {
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    private List<String> images;
    List<View> views = new ArrayList<>();
    private int index;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        UtilsStyle.setStatusBarMode(this,true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_gallery_preview);
        ButterKnife.bind(this);
        String erpRootUrl = LoginCache.getInstance().getLoginCache().getErpRootUrl() + "DataService/browseImage.aspx?autoid=";
        //String bcfHttpUrl = LoginCache.getInstance().getLoginCache().getBcfHttpUrl();
        images = getIntent().getStringArrayListExtra(SysConstant.msg_key_list1);
        String indexurl = getIntent().getStringExtra(SysConstant.msg_key_string1);
        for (int i = 0; i < images.size(); i++) {
           final ImageView imageView = new ImageView(this);
           // final PhotoView photoView = new (this);
            String s = images.get(i);
            if (s.equals(indexurl)) index = i;
            views.add(imageView);
            Glide.with(this).load(erpRootUrl + s).listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    imageView.setImageResource(R.drawable.error);
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    imageView.setImageResource(R.drawable.rc_loading);
                    return false;
                }
            }).into(imageView);
        }

        MyVPadapter padapter = new MyVPadapter();
        viewpager.setAdapter(padapter);
        viewpager.setCurrentItem(index);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            finish();
        }
        return super.onTouchEvent(event);
    }

    class MyVPadapter extends PagerAdapter {

        @Override
        public int getCount() {
            return images.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(views.get(position));
            return views.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }
}
